package com.djdevs.calculatorconverter;

//import android.annotation.TargetApi;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class FullscreenActivity extends Activity {

    Button button;

	private boolean _resultMode = false;
	private boolean _operatorReplace = false;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _resultMode = false;
        try {
        	setContentView(R.layout.activity_fullscreen);
            setConvButtonOnClickListener();
            SetBackSpaceOnClickListener();
            SetNumericButtonListener();
            SetBracesOnClickListener();
            SetDotOnClickListener();
            SetPlusMinusOnClickListener();
            SetPlusOnClickListener();
            SetMinusOnClickListener();
            SetMultiplyOnClickListener();
            SetDivideOnClickListener();
            SetEXPOnClickListener();
            SetSquareRootOnClickListener();
            SetCOnClickListener();
            SetCEOnClickListener();
            SetEqualsOnClickListener();
            SetNumReverseOnClickListener();
            throw new Exception();
		}
        catch (Exception e) {
        	
		}
    }
    
	private void SetEqualsOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnEquals);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        		RunEquals();
            }
        });
    }
    
    private void RunEquals()
    {
    	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
    	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
    	try {
	    	String txtList = etCalcList.getText().toString();
	    	String txt = HandleDotInText(ConvertDoubleZeroToIntZero(etCalc.getText().toString()));
	    	String result = "";
	    	double dblResult = 0.0;
	    	
	    	if (_resultMode) {
        		txtList = "";
        		_resultMode = false;
			}
	    	
	    	int openCounter = CountCharInString(txtList, '(');
	    	int closeCounter = CountCharInString(txtList, ')');
	    	
	    	if (!txt.equalsIgnoreCase("0")) {
	    		if (txt.startsWith("-") || txt.startsWith("√") || txt.startsWith("1/")) {
	    			txt = "(" + txt + ")";
	    			txtList = txtList + txt;
	    		}
	        	else
	        	{
	        		txtList = txtList + txt;
	        	}
	    	}
	    	
	    	if (openCounter > closeCounter) {
				int count = openCounter - closeCounter;
				while (count > 0) {
					txtList = txtList + ")";
					count--;
				}
			}
	    	
	    	if (txtList.indexOf("(") != -1) {
	    		result = CalculatorParser.CalculateBraces(txtList, txtList.indexOf("("));
	    		if (result.indexOf("(") == -1 && result.indexOf(")") == -1 && CalculatorParser.IfOperatorExists(result)) {
					dblResult = CalculatorParser.evaluate(result);
					if (dblResult != 0.0) {
		    			result = CalculatorParser.FormatNumber(dblResult) + "";
					}
		    		else
		    		{
		    			result = 0 + "";
		    		}
	    		}
	    		
	    		_resultMode = true;
		    	_operatorReplace = false;
		    	etCalcList.setText(txtList);
		    	etCalc.setText(result);
			}
	    	else {
	    		if (CalculatorParser.IfOperatorExists(txtList))
	    		{
		    		dblResult = CalculatorParser.evaluate(txtList);
		    		if (dblResult != 0.0) {
		    			result = CalculatorParser.FormatNumber(dblResult) + "";
					}
		    		else
		    		{
		    			result = 0 + "";
		    		}
		    		
		    		_resultMode = true;
			    	_operatorReplace = false;
			    	etCalcList.setText(txtList);
			    	etCalc.setText(result);
	    		}
	    		else
	    		{
	    			txtList = txt;
	    			_resultMode = true;
	    			_operatorReplace = false;
			    	etCalcList.setText(txtList);
	    	    	etCalc.setText(txt);
	    		}
			}
    	}
    	catch (Exception e) {
    		etCalcList.setText("");
        	etCalc.setText("undefined");
        	_resultMode = true;
        	_operatorReplace = false;
		}
    }
    
    private void SetCOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnC);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	_resultMode = false;
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	try {
	            	etCalc.setText(R.string.NUMERIC_0);
	            	etCalcList.setText("");
            	}
            	catch (Exception e)
            	{
            		//
            	}
            }
        });
    }
    
    private void SetCEOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnCE);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	_resultMode = false;
            	try {
            		etCalc.setText(R.string.NUMERIC_0);
            	}
            	catch (Exception e)
            	{
            		// To be handled
            	}
            }
        });
    }
    
    private void SetEXPOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnExp);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
	            	String txtList = etCalcList.getText().toString();
	            	String txt = HandleDotInText(ConvertDoubleZeroToIntZero(etCalc.getText().toString()));
	            	if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	if (txt.equalsIgnoreCase("0")) {
	            		if (!txtList.endsWith(")")) {
	            			txtList = ReplaceNumericOperator(txtList, "-", "^");
	                		txtList = ReplaceNumericOperator(txtList, "*", "^");
	                		txtList = ReplaceNumericOperator(txtList, "/", "^");
	                		txtList = ReplaceNumericOperator(txtList, "+", "^");
						}
	            		else {
	            			txtList = txtList + "^";
						}
					}
	            	else {
	            		if (_operatorReplace) {
	            			txtList = ReplaceNumericOperator(txtList, "-", "^");
	                		txtList = ReplaceNumericOperator(txtList, "*", "^");
	                		txtList = ReplaceNumericOperator(txtList, "/", "^");
	                		txtList = ReplaceNumericOperator(txtList, "+", "^");
						}
	            		else
	            		{
	            			if (txt.startsWith("-") || txt.startsWith("√") || txt.startsWith("1/")) {
		            			txt = "(" + txt + ")";
		    				}
		            		
		            		if (!txtList.endsWith(")")) {
		            			txtList = txtList + txt + "^";
		            		}
		            		else {
		            			txtList = txtList + "^";
							}
	            		}
	            		
					}
	            	_operatorReplace = true;
	            	etCalcList.setText(txtList);
	            	//etCalc.setText(R.string.NUMERIC_0);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
	            	etCalc.setText("undefined");
	            	_resultMode = false;
            	}
            }
        });
    }
    
    private void SetPlusOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnPlus);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
	            	String txtList = etCalcList.getText().toString();
	            	String txt = HandleDotInText(ConvertDoubleZeroToIntZero(etCalc.getText().toString()));
	            	if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	if (txt.equalsIgnoreCase("0")) {
	            		if (!txtList.endsWith(")")) {
	            			txtList = ReplaceNumericOperator(txtList, "-", "+");
	                		txtList = ReplaceNumericOperator(txtList, "*", "+");
	                		txtList = ReplaceNumericOperator(txtList, "/", "+");
	                		txtList = ReplaceNumericOperator(txtList, "^", "+");
						}
	            		else {
	            			txtList = txtList + btn.getText();
						}
					}
	            	else {
	            		if (_operatorReplace) {
	            			txtList = ReplaceNumericOperator(txtList, "-", "+");
	                		txtList = ReplaceNumericOperator(txtList, "*", "+");
	                		txtList = ReplaceNumericOperator(txtList, "/", "+");
	                		txtList = ReplaceNumericOperator(txtList, "^", "+");
	            		}
	            		else {
	            			if (txt.startsWith("-") || txt.startsWith("√") || txt.startsWith("1/")) {
		            			txt = "(" + txt + ")";
		    				}
		            		
		            		if (!txtList.endsWith(")")) {
		            			txtList = txtList + txt + btn.getText();
		            		}
		            		else {
		            			txtList = txtList + btn.getText();
							}
	            		}
					}
	            	_operatorReplace = true;
	            	etCalcList.setText(txtList);
	            	//etCalc.setText(R.string.NUMERIC_0);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
	            	etCalc.setText("undefined");
	            	_resultMode = false;
            	}
            }
        });
    }
    
    private void SetMinusOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnMinus);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
	            	String txtList = etCalcList.getText().toString();
	            	String txt = HandleDotInText(ConvertDoubleZeroToIntZero(etCalc.getText().toString()));
	            	if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	if (txt.equalsIgnoreCase("0")) {
	            		if (!txtList.endsWith(")")) {
	            			txtList = ReplaceNumericOperator(txtList, "+", "-");
	                		txtList = ReplaceNumericOperator(txtList, "*", "-");
	                		txtList = ReplaceNumericOperator(txtList, "/", "-");
	                		txtList = ReplaceNumericOperator(txtList, "^", "-");
						}
	            		else {
	            			txtList = txtList + btn.getText();
						}
					}
	            	else {
	            		if (_operatorReplace) {
	            			txtList = ReplaceNumericOperator(txtList, "+", "-");
	                		txtList = ReplaceNumericOperator(txtList, "*", "-");
	                		txtList = ReplaceNumericOperator(txtList, "/", "-");
	                		txtList = ReplaceNumericOperator(txtList, "^", "-");
	            		}
	            		else {
	            			if (txt.startsWith("-") || txt.startsWith("√") || txt.startsWith("1/")) {
		            			txt = "(" + txt + ")";
		    				}
		            		
		            		if (!txtList.endsWith(")")) {
		            			txtList = txtList + txt + btn.getText();
		            		}
		            		else {
		            			txtList = txtList + btn.getText();
							}
	            		}
					}
	            	_operatorReplace = true;
	            	etCalcList.setText(txtList);
	            	//etCalc.setText(R.string.NUMERIC_0);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
	            	etCalc.setText("undefined");
	            	_resultMode = false;
            	}
            }
        });
    }
    
    private void SetMultiplyOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnMultiply);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
	            	String txtList = etCalcList.getText().toString();
	            	String txt = HandleDotInText(ConvertDoubleZeroToIntZero(etCalc.getText().toString()));
	            	if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	if (txt.equalsIgnoreCase("0")) {
	            		if (!txtList.endsWith(")")) {
	            			txtList = ReplaceNumericOperator(txtList, "+", "*");
	                		txtList = ReplaceNumericOperator(txtList, "-", "*");
	                		txtList = ReplaceNumericOperator(txtList, "/", "*");
	                		txtList = ReplaceNumericOperator(txtList, "^", "*");
						}
	            		else {
	            			txtList = txtList + btn.getText();
						}
					}
	            	else {
	            		if (_operatorReplace) {
	            			txtList = ReplaceNumericOperator(txtList, "+", "*");
	                		txtList = ReplaceNumericOperator(txtList, "-", "*");
	                		txtList = ReplaceNumericOperator(txtList, "/", "*");
	                		txtList = ReplaceNumericOperator(txtList, "^", "*");
	            		}
	            		else {
	            			if (txt.startsWith("-") || txt.startsWith("√") || txt.startsWith("1/")) {
		            			txt = "(" + txt + ")";
		    				}
		            		
		            		if (!txtList.endsWith(")")) {
		            			txtList = txtList + txt + btn.getText();
		            		}
		            		else {
		            			txtList = txtList + btn.getText();
							}
	            		}
					}
	            	_operatorReplace = true;
	            	etCalcList.setText(txtList);
	            	//etCalc.setText(R.string.NUMERIC_0);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
	            	etCalc.setText("undefined");
	            	_resultMode = false;
            	}
            }
        });
    }
    
    private void SetDivideOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnDivision);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
	            	String txtList = etCalcList.getText().toString();
	            	String txt = HandleDotInText(ConvertDoubleZeroToIntZero(etCalc.getText().toString()));
	            	if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	if (txt.equalsIgnoreCase("0")) {
	            		if (!txtList.endsWith(")")) {
	            			txtList = ReplaceNumericOperator(txtList, "+", "/");
	                		txtList = ReplaceNumericOperator(txtList, "-", "/");
	                		txtList = ReplaceNumericOperator(txtList, "*", "/");
	                		txtList = ReplaceNumericOperator(txtList, "^", "/");
						}
	            		else {
	            			txtList = txtList + btn.getText();
						}
	            		
					}
	            	else {
	            		if (_operatorReplace) {
	            			txtList = ReplaceNumericOperator(txtList, "+", "/");
	                		txtList = ReplaceNumericOperator(txtList, "-", "/");
	                		txtList = ReplaceNumericOperator(txtList, "*", "/");
	                		txtList = ReplaceNumericOperator(txtList, "^", "/");
	            		}
	            		else {
	            			if (txt.startsWith("-") || txt.startsWith("√") || txt.startsWith("1/")) {
		            			txt = "(" + txt + ")";
		    				}
		            		
		            		if (!txtList.endsWith(")")) {
		            			txtList = txtList + txt + btn.getText();
		            		}
		            		else {
		            			txtList = txtList + btn.getText();
							}
	            		}
					}
	            	_operatorReplace = true;
	            	etCalcList.setText(txtList);
	            	//etCalc.setText(R.string.NUMERIC_0);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
            		etCalc.setText("undefined");
	            	_resultMode = false;
            	} 
            }
        });
    }
    
    private String ReplaceNumericOperator(String calcEquation, String op1, String op2)
    {
    	if (calcEquation.endsWith(op1)) {
			return calcEquation.substring(0, calcEquation.length() - 1).concat(op2);
		}
    	return calcEquation;
    }
      
    private void SetPlusMinusOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnPlusMinus);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
	            	String txt = HandleDotInText(ConvertDoubleZeroToIntZero(etCalc.getText().toString()));
	            	String txtList = etCalcList.getText().toString();
	            	
	            	if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	
	            	if (!txt.equalsIgnoreCase("0")) {
		            	if (txt.startsWith("-")) {
							txt = txt.substring(1);
						}
		            	else {
							txt = "-" + txt;
						}
	            	}
	            	
	            	etCalc.setText(txt);
	            	_operatorReplace = false;
	            	etCalcList.setText(txtList);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
            		etCalc.setText("undefined");
	            	_resultMode = false;
            	} 
            }
        });
    }
    
    private void SetNumReverseOnClickListener() 
    {
    	final Button btn = (Button)findViewById(R.id.btnNumericReverse);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
	            	String txt = HandleDotInText(ConvertDoubleZeroToIntZero(etCalc.getText().toString()));
	            	String txtList = etCalcList.getText().toString();
	            	
	            	if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	
	            	if (!txt.equalsIgnoreCase("0")) {
		            	if (txt.startsWith("1/")) {
							txt = txt.substring(2);
						}
		            	else {
							txt = "1/" + txt;
						}
	            	}
	            	
	            	etCalc.setText(txt);
	            	_operatorReplace = false;
	            	etCalcList.setText(txtList);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
            		etCalc.setText("undefined");
	            	_resultMode = false;
            	} 
            }
        });
		
	}
    
    private void SetSquareRootOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnSquareRoot);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
	            	String txt = HandleDotInText(ConvertDoubleZeroToIntZero(etCalc.getText().toString()));
	            	String txtList = etCalcList.getText().toString();
	            	
	            	if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	if (!txt.equalsIgnoreCase("0")) {
		            	if (txt.startsWith("√")) {
							txt = txt.substring(1);
						}
		            	else {
							txt = "√" + txt;
							
						}
	            	}
	            	
	            	etCalc.setText(txt);
	            	_operatorReplace = false;
	            	etCalcList.setText(txtList);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
            		etCalc.setText("undefined");
	            	_resultMode = false;
            	} 
            }
        });
    }

    
    private void SetBackSpaceOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnBckSpc);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	_resultMode = false;
            	try {
	            	String txt = ConvertDoubleZeroToIntZero(etCalc.getText().toString());
	            	txt = txt.substring(0, txt.length() - 1);
	            	if (txt.equalsIgnoreCase("-") || txt.equalsIgnoreCase("√") ||  txt.equalsIgnoreCase("1/") ||  txt.equalsIgnoreCase("")) {
	            		txt = 0 + "";
	            	}
	            	etCalc.setText(txt);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
            		etCalc.setText("undefined");
            	}
            }
        });
    }
    
    private void SetDotOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnDot);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	
            	try {
	            	String txt = ConvertDoubleZeroToIntZero(etCalc.getText().toString());
	            	String txtList = etCalcList.getText().toString();
	            	if (txt.indexOf(".") == -1) {
	            		txt = txt + btn.getText();
	            		etCalc.setText(txt);
					}
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
            		etCalc.setText("undefined");
            	}
            }
        });
    }
    
    private void SetBracesOnClickListener()
    {
    	final Button btn = (Button)findViewById(R.id.btnOpenBraces);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
	            	String txtList = etCalcList.getText().toString();
            		if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	if (txtList.endsWith("+") || txtList.endsWith("-") || txtList.endsWith("*") 
	            			|| txtList.endsWith("/") || txtList.endsWith("(") || txtList == "") {
						// Append Directly
	            		txtList = txtList + btn.getText();
	            		etCalcList.setText(txtList);
					}
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
            		_resultMode = false;
            	}
            }
        });
        
        final Button btnClose = (Button)findViewById(R.id.btnCloseBraces);
        btnClose.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	try {
	            	String txtList = etCalcList.getText().toString();
	            	String txt = ConvertDoubleZeroToIntZero(etCalc.getText().toString());
	            	if (_resultMode) {
	            		txtList = "";
	            		_resultMode = false;
					}
	            	
	            	int openCounter = CountCharInString(txtList, '(');
	            	int closeCounter = CountCharInString(txtList, ')');
	            	if (openCounter > closeCounter && txtList.indexOf("(") != -1) {
						//if (txtList.charAt(txtList.length() - 1) != '(') {
							if (!txt.equalsIgnoreCase("0")) {
								txtList = txtList + txt + btnClose.getText();
								etCalcList.setText(txtList);
								etCalc.setText(R.string.NUMERIC_0);
							}
							else {
								txtList = txtList + btnClose.getText();
								etCalcList.setText(txtList);
								etCalc.setText(R.string.NUMERIC_0);
							}
						//}
					}
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
            		etCalc.setText("undefined");
	            	_resultMode = false;
            	}
            }
        });
    }
    
    private int CountCharInString(String str, char c)
    {
    	int counter = 0;
    	for( int i=0; i<str.length(); i++ ) {
    	    if( str.charAt(i) == c ) {
    	        counter++;
    	    } 
    	}
    	
    	return counter;
    }
    
    private void SetNumericButtonListener()
    {
    	SetNumericButtonOnClickListener(R.id.btn0);
    	SetNumericButtonOnClickListener(R.id.btn1);
    	SetNumericButtonOnClickListener(R.id.btn2);
    	SetNumericButtonOnClickListener(R.id.btn3);
    	SetNumericButtonOnClickListener(R.id.btn4);
    	SetNumericButtonOnClickListener(R.id.btn5);
    	SetNumericButtonOnClickListener(R.id.btn6);
    	SetNumericButtonOnClickListener(R.id.btn7);
    	SetNumericButtonOnClickListener(R.id.btn8);
    	SetNumericButtonOnClickListener(R.id.btn9);
    }
    
    private void SetNumericButtonOnClickListener(int id)
    {
    	final Button btn = (Button)findViewById(id);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	TextView etCalc = (TextView)findViewById(R.id.txtCalc);
            	TextView etCalcList = (TextView)findViewById(R.id.txtCalcList);
            	try {
            		String txtList = etCalcList.getText().toString();
            		String txt = etCalc.getText().toString();
            		
            		if (_resultMode) {
						txt = "";
						txtList = "";
						etCalcList.setText(txtList);
						_resultMode = false;
					}
	            	
	            	if (txt.equalsIgnoreCase("0") || _operatorReplace) {
	            		txt = "";
	            		_operatorReplace = false;
					}
	            	
	            	txt = txt + btn.getText();
	            	etCalc.setText(txt);
            	}
            	catch (Exception e)
            	{
            		etCalcList.setText("");
            		etCalc.setText("undefined");
            	}
            }
        });
    }
    
    public String ConvertDoubleZeroToIntZero(String txt)
    {
    	if (txt.equalsIgnoreCase("0.0") || txt.equalsIgnoreCase("0.")) {
			return 0 + "";
		}
    	return txt;
    }
    
    public String HandleDotInText(String txt)
    {
    	if (txt.endsWith(".")) {
			return txt + 0;
		}
    	return txt;
    }

    public void setConvButtonOnClickListener() {
        final Button btn = (Button) findViewById(R.id.btnConv);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(FullscreenActivity.this, ConvActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }
}
